go to previous page   go to home page   go to next page

Answer:

An array of Card:

Card[] cards = new Card[12];

Array of Related Objects

Card[] cards = new Card[12];

cards[0] = new YouthBirthday( "Valerie", 7 );
cards[1] = new AdultBirthday( "Walter", 47 );
cards[2] = new Birthday( "Zoe", 30 );
cards[3] = new Holiday( "Kelly" );
cards[4] = new Valentine( "Jill", 42 );

for ( int j=0; j <= 4; j++ )
  cards[j].greeting();

Now any object that belongs in the hierarchy can fit into any slot of the array. This is like the greeting card display at the drug store that holds a different selection of cards in different seasons of the year.

This code will create 5 cards objects of various varieties and put them into the array. Then it will ask each object in the array to write out its own version of the greeting. This will work fine:

Dear Valerie,
Happy 7th Birthday
How you have grown!!

Dear Walter,
Happy 47th Birthday
You haven't changed at all!

Dear Zoe,
Happy 30th Birthday

Dear Kelly,
Season's Greetings!

Dear Jill,
Love and Kisses,
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

(I've eliminated some blank lines from the actual output.)


QUESTION 14:

Now say that this code follows the above:

Card temp;

temp     = cards[0];
cards[0] = cards[1];
cards[1] = temp;